// pages/orderForm/orderForm.js
const app = getApp()
var tongji = require('../../utils/tongji.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderInfo: '',
    inPay: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(e) {
    var that = this
    wx.request({
      url: app.urlData.getURL+'/index.php/mobile/Ordertry/order_detail',
      method: 'POST',
      data: {
        id: e.id,
      },
      header: {
        'Content-Type': 'application/json'
      },
      success: (res) => {
        console.log(res.data);
        that.setData({
          orderInfo: res.data
        })
      }
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  wxplay: function(e) {
    console.log(e);
    var that = this
    if (that.data.inPay == true) {
      that.setData({
        inPay: false
      })
      if (e.detail.errMsg == "getUserInfo:ok") {
        app.getUserInfo(wx.getStorageSync('sign'), e.detail.userInfo.nickName, e.detail.userInfo.avatarUrl, e.detail.userInfo.gender, e.detail.encryptedData, e.detail.iv)
        wx.request({
          url: app.urlData.getURL+'/index.php/mobile/Ordertry/orderTryPpay',
          method: 'POST',
          data: {
            user_id: wx.getStorageSync('user_id'),
            goods_id: that.data.orderInfo.goods_id,
            order_sn: that.data.orderInfo.order_sn
          },
          header: {
            'Content-Type': 'application/json'
          },
          success: (res) => {
            console.log(res);
            wx.requestPayment({
              'timeStamp': res.data.timeStamp,
              'nonceStr': res.data.nonceStr,
              'package': res.data.package,
              'signType': res.data.signType,
              'paySign': res.data.paySign,
              'success': function(res) {
                console.log(res);
                wx.showModal({
                  title: '恭喜您',
                  showCancel: false,
                  content: '已成功付款，请在系统消息中查看！',
                  success: function(res) {
                    console.log(res)
                    //这里写支付成功后操作
                    wx.navigateBack();
                  }
                })
              }
            })
          }
        })
      } else if (e.detail.errMsg == "getUserInfo:fail auth deny") {
        wx.showToast({
          title: '抱歉，拒绝授权将无法继续付款',
          icon: 'none',
          duration: 2500
        })
      }
      that.setData({
        inPay: true
      })
    }
  }
})